package com.controller;

import com.entity.*;
import com.github.pagehelper.Page;
import com.service.ReaderService;
import com.sun.org.apache.xml.internal.resolver.readers.ExtendedXMLCatalogReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.LinkedList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/reader")
public class ReaderController {

    @Autowired
    ReaderService readerService;

    //借书信息查询
    @RequestMapping("/bookDetail")
    public PageResult bookDetail(@RequestBody QueryPageBean queryPageBean, HttpServletRequest request){

        PageResult pageResult=readerService.queryPage(queryPageBean,request);
        return pageResult;
    }
    //借书
    @RequestMapping("/borrowBook")
    public Result borrowBook(Integer bNo,Integer brDay, HttpServletRequest request){
        try{
            readerService.borrowBook(bNo,brDay,request);
            return new Result(true,"借书成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"借书失败");
        }
    }



    //还书信息查询
    @RequestMapping("/getBorrowBooks")
    public PageResult returnBookDetail(@RequestBody QueryPageBean queryPageBean, HttpServletRequest request){
        PageResult pageResult=readerService.getBorrowBooks(queryPageBean,request);
        return pageResult;
    }
    //还书
    @RequestMapping("/returnBook")
    public Result returnBook(Integer bNo, HttpServletRequest request){
        try{
            readerService.returnBook(bNo,request);
            return new Result(true,"还书成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"还书失败");
        }
    }




    //书籍借阅信息查询
    @RequestMapping("/borrowDetail")
    public PageResult borrowDetail(@RequestBody QueryPageBean queryPageBean, HttpServletRequest request){
        PageResult pageResult=readerService.getBorrowDetail(queryPageBean,request);
        return pageResult;
    }
    //续借
    @RequestMapping("/borrowAgain")
    public Result borrowAgain(Integer bNo,HttpServletRequest request){
        try{
            readerService.borrowAgain(bNo,request);
            return new Result(true,"续借成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false,"续借失败");
        }

    }

}
